package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AsecuAuditService;
import cn.bdqn.pojo.Audit;
import cn.bdqn.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审计机构管理
 *
 * @author 8
 */
@Controller
//@RequestMapping("/audit")
public class AuditController {
    @Autowired
    AsecuAuditService auditService;

    @RequestMapping("/shenjijigou")
    public String shenjijigou(){
        return "/shenjijigou-list";
    }

    /**
     * 审计机构添加页面
     * @param model
     * @param content
     * @return
     */
    @RequestMapping("/shenjijigou-add")
    public String shenjijigouadd(Model model,Integer content){
        if (content!=0){
            Audit audit = auditService.getData1(content);
            model.addAttribute("content",audit.getAuditName());
            model.addAttribute("parent",audit.getId());
        }
        return "shenjijigou-add";
    }

    /**
     * 获取数据
     * @return
     */
    @RequestMapping("/getData")
    @ResponseBody
    public Map<String,Object> getData(){
        List<Audit> data = auditService.getData();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","ok");
        map.put("data",data);
        return map;
    }

    /**
     * 审计机构添加
     * @param audit
     * @return
     */
    @RequestMapping("/addShenjijigou")
    @ResponseBody
    public Map<String,Object> addShenjijigou(Audit audit){
        System.out.println("pid:"+audit.getParent());
        if (audit.getParent()==null){
            audit.setParent(0);
        }
        System.out.println(audit.getId()+"-"+audit.getAuditName()+"-"+audit.getCode()+"-"+audit.getParent());
        auditService.add(audit);
        Map map = new HashMap();
        map.put("status",200);
        return map;
    }

    /**
     * 审计机构修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/shenjijigou-edit")
//    @ResponseBody
    public String shenjijigouedit(Model model,Integer id){
        Audit data1 = auditService.getData1(id);
        Audit data2 = auditService.getData1(data1.getParent());
        if (data2==null){
            data2 = new Audit();
            data2.setAuditName("");
        }
        model.addAttribute("audit",data1);
        model.addAttribute("auditName",data2.getAuditName());
        return "shenjijigou-edit";
    }

    /**
     * 审计机构修改
     * @param audit
     * @return
     */
    @RequestMapping("/editShenjijigou")
    @ResponseBody
    public Map<String,Object> editShenjijigou(Audit audit){
        auditService.edit(audit);
        Map map = new HashMap();
        map.put("status",200);
        return map;
    }

    /**
     * 审计机构删除
     * @param id
     * @return
     */
    @RequestMapping("/delShenjijigou")
    @ResponseBody
    public Map<String,Object> delShenjijigou(Integer id){
        auditService.del(id);
        Map map = new HashMap();
        map.put("status",200);
        return map;
    }



    @RequestMapping("/getDeptData")
    @ResponseBody
    public List<Department> getDeptData(Integer auditId){
        System.out.println("");
        List<Department> list = auditService.getDeptData(auditId);
        return list;
    }
}
